package com.big.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.big.app.bo.PayBo;
import com.big.app.config.RedisConfig;
import com.big.app.dao.HotelDao;
import com.big.app.dto.HotelCommentDto;
import com.big.app.dto.HotelDto;
import com.big.app.entity.Hotel;
import com.big.app.entity.HotelOrder;
import com.big.app.service.intf.HotelService;
import com.big.app.util.AliPayUtils;
import com.big.app.util.JedisUtils;
import com.big.app.util.StringUtil;
import com.big.app.vo1.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * .::::.
 * .::::::::.
 * :::::::::::  I && YOU
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ````':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 */
@Service
public class HotelServiceImpl implements HotelService {
    //实例化日志对象
    private Logger logger= LoggerFactory.getLogger(HotelServiceImpl.class);
    @Resource
    private HotelDao dao;

    @Override
    public R queryHotTop() {
        logger.info("热门景点访问开始");
        List<HotelDto> list;
        //1.判断有缓存没
        if (JedisUtils.esists(RedisConfig.HOT_HOTEL)){
            logger.info("访问Redis");
            //2.将缓存数据字符串转换为json数组格式并传给集合.
            list=JSON.parseArray(JedisUtils.getStr(RedisConfig.HOT_HOTEL),HotelDto.class);
            logger.info("Redis访问结束");
        }else {
            //没有缓存
            logger.info("访问数据库");
            list=dao.queryHotTop();
            if(list==null){
                return R.fail("暂无热门酒店");
            }
            logger.info("数据库访问结束");
        }
        logger.info("访问结束");
        return R.ok(list);
    }

    public R search(String title){
        if (title!=null){
            title="%"+title+"%";
            List<HotelDto> list=dao.search(title);
            if (list!=null){
                return R.ok(list);
            }else {
                return R.fail("没有查到相关内容!");
            }
        }
        return R.fail("搜索不能为空");
    }

    public R detail (int id){
        if (id!=0){
            Hotel hotel = dao.selectById(id);
            if (hotel!=null){
                return R.ok(hotel);
            }else {
                return R.fail("网络丢失了~");
            }
        }
        return R.fail("查看异常");
    }

    @Transactional
    @Override
    public R comment(int id,int uid) {
        if (id>0&&uid>0) {
            List<HotelCommentDto> list = dao.queryComment(id);
            if (list != null) {
                int hid=id;
                dao.hotelLooks(uid,hid);
                return R.ok(list);
            } else {
                return R.fail("暂无评论！");
            }
        }
        return R.fail("查看出错了！");
    }

    @Override
    public R order(PayBo bo) {
        //1.验证参数
        if(bo!=null){
            //2.生成 订单号 长的
            String no=createNo();
            //构建支付对象
            System.out.println(bo.getPrice());
            AliPayUtils.AliPay pay=new AliPayUtils.AliPay(no, bo.getPrice(), bo.getRoom());
            //发起支付
            String content= AliPayUtils.createPay(pay);
            if(StringUtil.isNoyEmpty(content)){
                //创建支付成功
                //存储到数据库
                HotelOrder obj=new HotelOrder(no,bo.getHid(),bo.getRoom(),bo.getTime(),bo.getPrice(),bo.getUid());
                dao.orderInsert(obj);
                //返回 - 支付宝生成的表单内容
                return R.ok(content);
            }
        }
        return R.fail("网络异常！");
    }
//生成订单号，不超过64位
private String createNo(){
    return System.currentTimeMillis()+"_"+new Random().nextInt(1000);
}
}
